<template>

    <div class="content-item sync-tags">
        <h2>Tags (optional)</h2>
        <label>
            <input type="text" @keyup.enter="insertTags($event)" @blur="insertTags($event)" :disabled="tags.length >= 10" />
            <i class="iconfont icon-jiahao"></i>
        </label>
        <span v-for="(tag, index) in tags" :key="tag + index">
            <em v-text="tag"></em>
            <i class="iconfont icon-guanbi" @click="tags = tags.filter((a, i) => i != index)"></i>
        </span>
    </div>

</template>
<script>
import { mapGetters, mapState } from "vuex";
export default {
    name: "SynchronizeCollection",
    props: {
        value: { type: String, default: () => '' },
    },
    data () {
        return {
            loading: true,
            collections: [],
        };
    },
    computed: {
        // ...mapState("synchronize", {
        //     shop: "shop",
        //     setting: "setting",
        // }),
        tags: {
            get () {

                if (this.value && this.value.split) {
                    return this.value.split('|').filter(i => i)
                } else {
                    return []
                }
            },
            set (val) {
                const tags = val.join ? val.join("|") : "";
                if (this.value != tags) {
                    this.$emit('input', tags);
                }
            }
        }
    },


    methods: {
        insertTags (event) {
            let value = event.target.value;
            if (value) {
                this.tags = [value].concat(this.tags);
                event.target.value = "";
                document.activeElement.blur();
            }
        },


    },
    created () {
    },
};
</script>
<style lang="scss" scoped>
.sync-tags {
    display: flex;
    flex-wrap: wrap;
    margin: 0 -9px;

    h2 {
        box-sizing: border-box;
        width: 100%;
        padding-left: 9px;
        padding-right: 9px;
        padding-bottom: 7px;
    }

    label,
    span {
        min-width: calc(20% - 18px);
        margin: 9px;
    }

    label {
        position: relative;
        input {
            box-sizing: border-box;
            padding: 5px 12px;
            width: 100%;
            height: 40px;
            background: #ffffff;
            border-radius: 4px;
            border: 1px solid #dcdcdc;
            line-height: 18px;
            cursor: pointer;

            &:hover {
                border-color: #ea4c89;
                outline: none;
                & + i {
                    background-color: #ea4c89;
                }
            }

            &:focus {
                border-color: #ea4c89;
                outline: none;
                cursor: text;
            }

            &:focus + i {
                display: none;
            }

            &:disabled {
                background: #f6f6f6;
                border-color: #dcdcdc;
                color: #919191;
                cursor: default;

                &:hover {
                    background: #fafafa;
                    color: #666;
                }

                & + i {
                    background-color: #dcdcdc;
                }
            }
        }
        i {
            position: absolute;
            width: 18px;
            height: 18px;
            line-height: 18px;
            text-align: center;
            background-color: #000000;
            border-radius: 18px;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            pointer-events: none;
            color: #fff;

            &::before {
                content: '';
                position: absolute;
                width: 10px;
                height: 2px;
                background-color: #fff;
                border-radius: 2px;
                left: 50%;
                top: 50%;
                transform: translate(-50%, -50%);
            }
            &::after {
                content: '';
                position: absolute;
                width: 2px;
                height: 10px;
                background-color: #fff;
                border-radius: 2px;
                left: 50%;
                top: 50%;
                transform: translate(-50%, -50%);
            }
        }
    }

    span {
        cursor: default;
        border: 1px solid #dcdcdc;
        height: 40px;
        line-height: 38px;
        box-sizing: border-box;
        padding: 0 12px;
        border-radius: 4px;
        position: relative;
        font-size: 14px;
        color: #000000;

        i {
            width: 24px;
            height: 24px;
            line-height: 24px;
            text-align: center;
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            right: 4px;
            cursor: pointer;

            &:hover {
                color: #ea4c89;
            }
        }
    }
}
</style>
